/**
 * 流程表单设计插件
 */
var finputFn = {
	editor: null,
	initEditor:function(){
		this.editor = UE.getEditor('formEditor',{
			toolleipi: true,
		    /*toolbars:[[
		        'fullscreen', 'source', '|', 'undo', 'redo', '|','bold', 'italic', 'underline', 'fontborder', 'strikethrough',  'removeformat', '|', 'forecolor', 'backcolor', 'insertorderedlist', 'insertunorderedlist','|', 'fontfamily', 'fontsize', '|', 'indent', '|', 'justifyleft', 'justifycenter', 'justifyright', 'justifyjustify', '|',  'link', 'unlink',  '|',  'horizontal',  'spechars',  'wordimage', '|', 'inserttable', 'deletetable',  'mergecells',  'splittocells'
		    ]],*/
		    wordCount:false,
		    elementPathEnabled:false,
		    disabledTableInTable: false,
		    initialFrameHeight:500
		});
	},
	exec:function (method,row) {
		this.editor.execCommand(method,row);
	},
	submitForm:function(){
		var editor = this.editor;
		if(editor){
			if(editor.queryCommandState('source')){
				editor.execCommand('source');//模式切换
			}
			editor.sync();
			
			var formConfig = editor.getContent();
			console.log(formConfig);
			
			var url = '/console/jForm/rewrite/designSave' ;
			var data = { formId: $("input[name=formId]").val(), formConfig: formConfig };
			$.post(url,data,function(result){
				if(result && result.flag){
					alert('保存成功！');
				}
			});
		}
	},
	popupDelete:function(el,popup){
		var finput = el.getAttribute('finput'),
			fplug = el.getAttribute('fplug'),
			ftitle = el.getAttribute('ftitle'),
			fname = el.getAttribute('fname');
        ftitle  += ("&nbsp;" + fname);
		if ( /input|textarea|table|select/ig.test( el.tagName ) && finput=='Y') {
			var html = popup.formatHtml('<nobr>'+ fplug +': '+ ftitle +'&nbsp;&nbsp;<span onclick=$$._delete() class="edui-clickable">删除</span></nobr>' );
			if ( html ) {
				popup.getDom( 'content' ).innerHTML = html;
				popup.anchorEl = el;
				popup.showAnchor( popup.anchorEl );
			} else {
				popup.hide();
			}
		}
	},
	formartAttrs: function(row){
		var attrs = {finput:'Y',fname:row.fieldName,ftitle: row.fieldTitle,fplug: row.fieldPlug,type:'text',id:row.fieldName };
		var rowAttrs = {
		     type:'element',
		     tagName:'input',
		     attrs: attrs
		 };
		switch (row.fieldPlug) {
		case '隐藏属性':
		case '单行文本':
		case '数字输入':
		case '自动编号':
		case '日期选择':
		case '时间选择':
		case '富文本':
		case '数据字典':
		case '附件上传':
		case '人员选择':
		case '部门选择':
		case '动作按钮':
		case '关联子表':
			break;
		case '多行文本':
			rowAttrs.tagName = 'textarea';
			rowAttrs.attrs.type = 'textarea';
			rowAttrs.attrs.style= 'width:120px;';
			break;
		default:
			break;
		}
		rowAttrs.attrs = attrs ;
		
		return rowAttrs;
	},
	addListener:function(me,popup){
		var _this = this;
		popup.render();
		me.addListener( 'mouseover', function( t, evt ) {
			evt = evt || window.event;
			var el = evt.target || evt.srcElement;
			_this.popupDelete(el, popup);
		});
	}
};

UE.plugins['隐藏属性'] = function () {
	var me = this,thePlugins = '隐藏属性';
	me.commands[thePlugins] = {
		execCommand:function (mm,row) {
			var nodeAtts = finputFn.formartAttrs(row) ;
			var node = new UE.uNode(nodeAtts);
			me.execCommand('insertHtml',node.toHtml());
		}
	};
	var popup = new baidu.editor.ui.Popup({
		editor:this,
		content: '',
		className: 'edui-bubble',
		_delete:function(){
			if( window.confirm('确认删除该控件吗？') ) {
				baidu.editor.dom.domUtils.remove(this.anchorEl,false);
			}
			this.hide();
		}
	} );
	finputFn.addListener(me, popup);
};

UE.plugins['数字输入'] = function () {
	var me = this,thePlugins = '数字输入';
	me.commands[thePlugins] = {
		execCommand:function (mm,row) {
			var nodeAtts = finputFn.formartAttrs(row) ;
			var node = new UE.uNode(nodeAtts);
			me.execCommand('insertHtml',node.toHtml());
		}
	};
	var popup = new baidu.editor.ui.Popup({
		editor:this,
		content: '',
		className: 'edui-bubble',
		_delete:function(){
			if( window.confirm('确认删除该控件吗？') ) {
				baidu.editor.dom.domUtils.remove(this.anchorEl,false);
			}
			this.hide();
		}
	} );
	finputFn.addListener(me, popup);
};

UE.plugins['单行文本'] = function () {
	var me = this,thePlugins = '单行文本';
	me.commands[thePlugins] = {
		execCommand:function (mm,row) {
			var nodeAtts = finputFn.formartAttrs(row) ;
			var node = new UE.uNode(nodeAtts);
			me.execCommand('insertHtml',node.toHtml());
		}
	};
	var popup = new baidu.editor.ui.Popup({
		editor:this,
		content: '',
		className: 'edui-bubble',
		_delete:function(){
			if( window.confirm('确认删除该控件吗？') ) {
				baidu.editor.dom.domUtils.remove(this.anchorEl,false);
			}
			this.hide();
		}
	} );
	finputFn.addListener(me, popup);
};

UE.plugins['自动编号'] = function () {
	var me = this,thePlugins = '自动编号';
	me.commands[thePlugins] = {
		execCommand:function (mm,row) {
			var nodeAtts = finputFn.formartAttrs(row) ;
			var node = new UE.uNode(nodeAtts);
			me.execCommand('insertHtml',node.toHtml());
		}
	};
	var popup = new baidu.editor.ui.Popup({
		editor:this,
		content: '',
		className: 'edui-bubble',
		_delete:function(){
			if( window.confirm('确认删除该控件吗？') ) {
				baidu.editor.dom.domUtils.remove(this.anchorEl,false);
			}
			this.hide();
		}
	} );
	finputFn.addListener(me, popup);
};


UE.plugins['人员选择'] = function () {
	var me = this,thePlugins = '人员选择';
	me.commands[thePlugins] = {
		execCommand:function (mm,row) {
			var nodeAtts = finputFn.formartAttrs(row) ;
			var node = new UE.uNode(nodeAtts);
			me.execCommand('insertHtml',node.toHtml());
		}
	};
	var popup = new baidu.editor.ui.Popup({
		editor:this,
		content: '',
		className: 'edui-bubble',
		_delete:function(){
			if( window.confirm('确认删除该控件吗？') ) {
				baidu.editor.dom.domUtils.remove(this.anchorEl,false);
			}
			this.hide();
		}
	} );
	finputFn.addListener(me, popup);
};

UE.plugins['部门选择'] = function () {
	var me = this,thePlugins = '部门选择';
	me.commands[thePlugins] = {
		execCommand:function (mm,row) {
			var nodeAtts = finputFn.formartAttrs(row) ;
			var node = new UE.uNode(nodeAtts);
			me.execCommand('insertHtml',node.toHtml());
		}
	};
	var popup = new baidu.editor.ui.Popup({
		editor:this,
		content: '',
		className: 'edui-bubble',
		_delete:function(){
			if( window.confirm('确认删除该控件吗？') ) {
				baidu.editor.dom.domUtils.remove(this.anchorEl,false);
			}
			this.hide();
		}
	} );
	finputFn.addListener(me, popup);
};

UE.plugins['日期选择'] = function () {
	var me = this,thePlugins = '日期选择';
	me.commands[thePlugins] = {
		execCommand:function (mm,row) {
			var nodeAtts = finputFn.formartAttrs(row) ;
			var node = new UE.uNode(nodeAtts);
			me.execCommand('insertHtml',node.toHtml());
		}
	};
	var popup = new baidu.editor.ui.Popup({
		editor:this,
		content: '',
		className: 'edui-bubble',
		_delete:function(){
			if( window.confirm('确认删除该控件吗？') ) {
				baidu.editor.dom.domUtils.remove(this.anchorEl,false);
			}
			this.hide();
		}
	} );
	finputFn.addListener(me, popup);
};

UE.plugins['时间选择'] = function () {
	var me = this,thePlugins = '时间选择';
	me.commands[thePlugins] = {
		execCommand:function (mm,row) {
			var nodeAtts = finputFn.formartAttrs(row) ;
			var node = new UE.uNode(nodeAtts);
			me.execCommand('insertHtml',node.toHtml());
		}
	};
	var popup = new baidu.editor.ui.Popup({
		editor:this,
		content: '',
		className: 'edui-bubble',
		_delete:function(){
			if( window.confirm('确认删除该控件吗？') ) {
				baidu.editor.dom.domUtils.remove(this.anchorEl,false);
			}
			this.hide();
		}
	} );
	finputFn.addListener(me, popup);
};


UE.plugins['数字输入'] = function () {
	var me = this,thePlugins = '数字输入';
	me.commands[thePlugins] = {
		execCommand:function (mm,row) {
			var nodeAtts = finputFn.formartAttrs(row) ;
			var node = new UE.uNode(nodeAtts);
			me.execCommand('insertHtml',node.toHtml());
		}
	};
	var popup = new baidu.editor.ui.Popup({
		editor:this,
		content: '',
		className: 'edui-bubble',
		_delete:function(){
			if( window.confirm('确认删除该控件吗？') ) {
				baidu.editor.dom.domUtils.remove(this.anchorEl,false);
			}
			this.hide();
		}
	} );
	finputFn.addListener(me, popup);
};

UE.plugins['多行文本'] = function () {
	var me = this,thePlugins = '多行文本';
	me.commands[thePlugins] = {
		execCommand:function (mm,row) {
			var nodeAtts = finputFn.formartAttrs(row) ;
			var node = new UE.uNode(nodeAtts);
			me.execCommand('insertHtml',node.toHtml());
		}
	};
	var popup = new baidu.editor.ui.Popup({
		editor:this,
		content: '',
		className: 'edui-bubble',
		_delete:function(){
			if( window.confirm('确认删除该控件吗？') ) {
				baidu.editor.dom.domUtils.remove(this.anchorEl,false);
			}
			this.hide();
		}
	} );
	finputFn.addListener(me, popup);
};

UE.plugins['数据字典'] = function () {
	var me = this,thePlugins = '数据字典';
	me.commands[thePlugins] = {
		execCommand:function (mm,row) {
			//var attrs = finputFn.formartAttrs(row);
			//attrs['style'] = "width:120px;";
			var nodeAtts = finputFn.formartAttrs(row) ;
			nodeAtts.attrs.url = row.fieldDataSource ;
			nodeAtts.attrs.type = 'select' ;
			nodeAtts.attrs.style = 'width:173px;' ;
			nodeAtts.tagName = 'select' ;
			
			var node = new UE.uNode(nodeAtts);
			me.execCommand('insertHtml',node.toHtml());
		}
	};
	var popup = new baidu.editor.ui.Popup({
		editor:this,
		content: '',
		className: 'edui-bubble',
		_delete:function(){
			if( window.confirm('确认删除该控件吗？') ) {
				baidu.editor.dom.domUtils.remove(this.anchorEl,false);
			}
			this.hide();
		}
	} );
	finputFn.addListener(me, popup);
};

UE.plugins['附件上传'] = function () {
	var me = this,thePlugins = '附件上传';
	me.commands[thePlugins] = {
		execCommand:function (mm,row) {
			var nodeAtts = finputFn.formartAttrs(row) ;
			var node = new UE.uNode(nodeAtts);
			me.execCommand('insertHtml',node.toHtml());
		}
	};
	var popup = new baidu.editor.ui.Popup({
		editor:this,
		content: '',
		className: 'edui-bubble',
		_delete:function(){
			if( window.confirm('确认删除该控件吗？') ) {
				baidu.editor.dom.domUtils.remove(this.anchorEl,false);
			}
			this.hide();
		}
	} );
	finputFn.addListener(me, popup);
};


UE.plugins['关联子表'] = function () {
	var me = this,thePlugins = '关联子表';
	me.commands[thePlugins] = {
		execCommand:function (mm,row) {
			var nodeAtts = finputFn.formartAttrs(row);
			//var node = new UE.uNode(nodeAtts);
			//me.execCommand('insertHtml',node.toHtml());
			 $.get("/console/jForm/rewrite/getJformByTableName",{tableName: row.fieldDataSource}, function(result){
				 if(result&&result.flag){
					 var root = UE.htmlparser(result.data.formConfig, true);
					 var table = root.getNodesByTagName('table')[0];
					 $.each(nodeAtts.attrs,function(name,value) {
						 table.setAttr(name,value);
					 });
					 me.execCommand('insertHtml',table.toHtml());
				 }
			 });
		}
	};
	var popup = new baidu.editor.ui.Popup({
		editor:this,
		content: '',
		className: 'edui-bubble',
		_delete:function(){
			if( window.confirm('确认删除该控件吗？') ) {
				baidu.editor.dom.domUtils.remove(this.anchorEl,false);
			}
			this.hide();
		}
	} );
	finputFn.addListener(me, popup);
};

UE.plugins['添加行按钮'] = function () {
	var me = this,thePlugins = '添加行按钮';
	me.commands[thePlugins] = {
		execCommand:function (mm,row) {
			var nodeAtts = finputFn.formartAttrs(row);
			nodeAtts.tagName = 'button';
			nodeAtts.attrs.type= 'button';
			var button = new UE.uNode(nodeAtts);
			button.innerHTML(row.fieldTitle);
			me.execCommand('insertHtml',button.toHtml());
		}
	};
	var popup = new baidu.editor.ui.Popup({
		editor:this,
		content: '',
		className: 'edui-bubble',
		_delete:function(){
			if( window.confirm('确认删除该控件吗？') ) {
				baidu.editor.dom.domUtils.remove(this.anchorEl,false);
			}
			this.hide();
		}
	} );
	finputFn.addListener(me, popup);
};

UE.plugins['删除行按钮'] = function () {
	var me = this,thePlugins = '删除行按钮';
	me.commands[thePlugins] = {
		execCommand:function (mm,row) {
			var nodeAtts = finputFn.formartAttrs(row);
			nodeAtts.tagName = 'button';
			nodeAtts.attrs.type= 'button';
			var button = new UE.uNode(nodeAtts);
			button.innerHTML(row.fieldTitle);
			me.execCommand('insertHtml',button.toHtml());
		}
	};
	var popup = new baidu.editor.ui.Popup({
		editor:this,
		content: '',
		className: 'edui-bubble',
		_delete:function(){
			if( window.confirm('确认删除该控件吗？') ) {
				baidu.editor.dom.domUtils.remove(this.anchorEl,false);
			}
			this.hide();
		}
	} );
	finputFn.addListener(me, popup);
};


/*按钮扩展*/
UE.registerUI('button_template',function(editor,uiName){
    if(!this.options.toolleipi)
    {
        return false;
    }
    //注册按钮执行时的command命令，使用命令默认就会带有回退操作
    editor.registerCommand(uiName,{
        execCommand:function(){
            try {
                //formDesign.exec('leipi_template');
                //formDesign.fnCheckForm('save');
            } catch ( e ) {
                alert('打开模板异常');
            }
            
        }
    });
    //创建一个button
    var btn = new UE.ui.Button({
        //按钮的名字
        name:uiName,
        //提示
        title:"表单模板",
        //需要添加的额外样式，指定icon图标，这里默认使用一个重复的icon
        cssRules :'background-position: -339px -40px;',
        //点击时执行的命令
        onclick:function () {
            //这里可以不用执行命令,做你自己的操作也可
           editor.execCommand(uiName);
        }
    });

    //因为你是添加button,所以需要返回这个button
    return btn;
});

UE.registerUI('button_preview',function(editor,uiName){
    if(!this.options.toolleipi)
    {
        return false;
    }
    //注册按钮执行时的command命令，使用命令默认就会带有回退操作
    editor.registerCommand(uiName,{
        execCommand:function(){
            try {
            	var dialog = new UE.ui.Dialog({
    				iframeUrl:this.options.UEDITOR_HOME_URL+'/dialogs/preview/preview.html',
    				name:'asdfasdf',
    				editor:this,
    				title: '文本框',
    				cssRules:"width:600px;height:310px;",
    				buttons:[
    				{
    					className:'edui-okbutton',
    					label:'确定',
    					onclick:function () {
    						dialog.close(true);
    					}
    				},
    				{
    					className:'edui-cancelbutton',
    					label:'取消',
    					onclick:function () {
    						dialog.close(false);
    					}
    				}]
    			});
    			dialog.render();
    			dialog.open();
                //formDesign.fnReview();
            } catch ( e ) {
                alert('formDesign.fnReview 预览异常');
            }
        }
    });
    //创建一个button
    var btn = new UE.ui.Button({
        //按钮的名字
        name:uiName,
        //提示
        title:"表单设计预览",
        //需要添加的额外样式，指定icon图标，这里默认使用一个重复的icon
        cssRules :'background-position: -420px -19px;',
        //点击时执行的命令
        onclick:function () {
            //这里可以不用执行命令,做你自己的操作也可
           editor.execCommand(uiName);
        }
    });
    return btn;
});

UE.registerUI('button_save',function(editor,uiName){
    if(!this.options.toolleipi)
    {
        return false;
    }
    //注册按钮执行时的command命令，使用命令默认就会带有回退操作
    editor.registerCommand(uiName,{
        execCommand:function(){
            try {
            	finputFn.submitForm();
            } catch ( e ) {
                alert('formDesign.fnCheckForm("save") 保存异常');
            }
        }
    });
    //创建一个button
    var btn = new UE.ui.Button({
        //按钮的名字
        name:uiName,
        //提示
        title:"保存表单",
        //需要添加的额外样式，指定icon图标，这里默认使用一个重复的icon
        cssRules :'background-position: -481px -20px;',
        //点击时执行的命令
        onclick:function () {
            //这里可以不用执行命令,做你自己的操作也可
           editor.execCommand(uiName);
        }
    });

    //因为你是添加button,所以需要返回这个button
    return btn;
});